package com.cy.src.update.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.cy.src.update.download.Config;
import com.cy.src.update.parser.DefaultXmlInfo;
import com.cy.src.update.parser.DefaultXmlParser;
import com.cy.src.update.parser.MeshXmlParser;

/**
 * Created by liwg on 2017/10/31.
 */

public class CheckUpdate  implements DefaultXmlParser.OnDefaultXmlParserListener {
    private Context mContext;
    private ProgressDialog mProgressDialog;
    private DefaultXmlInfo mXmlInfo;
    private Config mConfig;
    private MyHandler mHandler;
    private final int WHAT_RESULT;
    private final int WHAT_ERROR;
    private final int WHAT_TOAST;
    private UpdateTask.OnUpdateListener mOnUpdateListener;

    public CheckUpdate(Context context, Config config) {
        this(context, config, (UpdateTask.OnUpdateListener)null);
    }

    public CheckUpdate(Context context, Config config, UpdateTask.OnUpdateListener listener) {
        this.WHAT_RESULT = 1;
        this.WHAT_ERROR = 2;
        this.WHAT_TOAST = 3;
        this.mContext = context;
        this.mConfig = config;
        this.mOnUpdateListener = listener;
        this.mHandler = new MyHandler();
    }

    public void startRequest() {
        this.init();
    }

    public void startRequest(MeshXmlParser xmlParser) {
        String url = this.mConfig.getUpdateUrl();
        if(!TextUtils.isEmpty(url) && url.startsWith("http")) {
            if(!this.mConfig.isSilenceUpdate()) {
                this.showProgressDialog();
            }

            xmlParser.setUrl(url);
            xmlParser.setListener(this);
        } else {
            throw new IllegalArgumentException("url 不合法");
        }
    }

    private void init() {
        String url = this.mConfig.getUpdateUrl();
        if(!TextUtils.isEmpty(url) && url.startsWith("http")) {
            if(!this.mConfig.isSilenceUpdate()) {
                this.showProgressDialog();
            }

            new DefaultXmlParser(url, this);
        } else {
            throw new IllegalArgumentException("url 不合法");
        }
    }

    private void showProgressDialog() {
        this.mProgressDialog = new ProgressDialog(this.mContext);
        this.mProgressDialog.getWindow().setFlags(2008, 2008);
        this.mProgressDialog.setMessage("正在玩命请求，请稍后...");
        this.mProgressDialog.show();
    }

    private void removeProgressDialog() {
        if(this.mProgressDialog != null && this.mProgressDialog.isShowing()) {
            this.mProgressDialog.cancel();
        }

    }

    private void dealResult(DefaultXmlInfo info) {
        this.removeProgressDialog();
        int remoteVersion = Integer.parseInt(info.getAndroidversion());
        int localVersion = this.getVersionCode();
        if(remoteVersion > localVersion) {
            this.updateWindow();
        } else if(!this.mConfig.isSilenceUpdate()) {
            Toast.makeText(this.mContext, "您使用的是最新版本，无需更新。", 0).show();
        }

        Log.w("", "DefaultXmlInfo  " + info.getAndroidurl() + "  " + remoteVersion + "   " + localVersion);
    }

    private void updateWindow() {
        CheckUpdateActivity.startActivity(this.mContext, this.mConfig, this.mXmlInfo);
    }

    public void result(DefaultXmlInfo info) {
        this.mXmlInfo = info;
        this.mHandler.sendEmptyMessage(1);
    }

    public void onNetworkError() {
        this.mHandler.sendEmptyMessage(2);
    }

    private int getVersionCode() {
        try {
            PackageInfo e = this.mContext.getPackageManager().getPackageInfo(this.mContext.getPackageName(), 1);
            return e.versionCode;
        } catch (PackageManager.NameNotFoundException var2) {
            var2.printStackTrace();
            return 0;
        }
    }

    public interface OnUpdateListener {
        void versionIsLast();
    }

    private class MyHandler extends Handler {
        private MyHandler() {
        }

        public void handleMessage(Message msg) {
            switch(msg.what) {
                case 1:
                    CheckUpdate.this.dealResult(CheckUpdate.this.mXmlInfo);
                    break;
                case 2:
                    Log.d("Update", "请求失败");
                    CheckUpdate.this.removeProgressDialog();
                    break;
                case 3:
                    if(msg.obj != null) {
                        Log.d("Update", msg.obj.toString() + "");
                    }
            }

        }
    }
}
